package com.leo;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @ClassName Summary
 * @Description 总结：
 * @Author wangss
 * @date 2020.05.26 22:13
 * @Version 1.0
 */
public class Summary3 extends Application {

    private double x = 0.0;
    private double y = 0.0;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(anchorPane);

        Button button1 = new Button("button1");

        Button button2 = new Button("button2");

        AnchorPane.setTopAnchor(button2, 100d);
        AnchorPane.setLeftAnchor(button2, 100d);
        anchorPane.getChildren().addAll(button2, button1);

        primaryStage.setScene(scene);
        primaryStage.setTitle("spinner");
        primaryStage.setWidth(625d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        Bounds bounds2 = button2.getLayoutBounds();

        Point2D point1 = button2.localToParent(bounds2.getMinX(), bounds2.getMinY());
        Point2D point2 = button2.localToParent(bounds2.getMaxX(), bounds2.getMaxY());

        System.out.println(point1);
        System.out.println(point2);

        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {

            Point2D point3 = null;
            Point2D point4 = null;
            Bounds bounds1 = button1.getLayoutBounds();

            @Override
            public void handle(KeyEvent event) {
                if (event.getCode().getName().equals(KeyCode.X.getName())) {
                    x = x + 10;
                    button1.setLayoutX(x);

                    point3 = button1.localToParent(bounds1.getMinX(), bounds1.getMinY());
                    point4 = button1.localToParent(bounds1.getMaxX(), bounds1.getMaxY());
                } else if (event.getCode().getName().equals(KeyCode.Y.getName())) {
                    y = y + 10;
                    button1.setLayoutY(y);

                    point3 = button1.localToParent(bounds1.getMinX(), bounds1.getMinY());
                    point4 = button1.localToParent(bounds1.getMaxX(), bounds1.getMaxY());
                }

                if (point3 == null || point4 == null) {
                    return;
                }

                if (point4.getX() >= point1.getX() && point4.getY() >= point1.getY()) {
                    if (point4.getX() <= point2.getX() && point4.getY() <= point2.getY()) {
                        System.out.println("相撞了");
                        return;
                    }
                }

                if (point3.getX() >= point1.getX() && point3.getY() >= point1.getY()) {
                    if (point3.getX() <= point2.getX() && point3.getY() <= point2.getY()) {
                        System.out.println("相撞了");
                        return;
                    }
                }
            }
        });
    }
}
